#ifndef QLOGGER_H
#define QLOGGER_H

#include <QDateTime>
#include <QObject>

#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"

class QLogger : public QObject {
  Q_OBJECT

public:
  typedef spdlog::level::level_enum Level;
  enum LogMode { Console = 0x01, File = 0x02, ALL = 0x03 };

  static void Error(const QString &mesg) { spdlog::error(mesg.toStdString()); }
  static void Warn(const QString &mesg) { spdlog::warn(mesg.toStdString()); }
  static void Info(const QString &mesg) { spdlog::info(mesg.toStdString()); }
  static void Debug(const QString &mesg) { spdlog::debug(mesg.toStdString()); }

  Q_DECLARE_FLAGS(LogModes, LogMode)

  QLogger(const QString &name, const LogModes &modes = ALL,
          QObject *parent = nullptr);
  ~QLogger();

  static QString TimeStamp;
  static std::shared_ptr<spdlog::sinks::stdout_color_sink_mt> ConsoleSink;
  static std::shared_ptr<spdlog::sinks::basic_file_sink_mt> FileSink;
  static void setSavePath(const QString &path);
  static void setModeLevel(const Level &level, const LogModes &modes = ALL);
  static void init();
  void setLevel(const Level &level);

public slots:
  void error(const QString &mesg);
  void info(const QString &mesg);
  void warn(const QString &mesg);
  void debug(const QString &mesg);

private:
  static void checkInit();
  static QString mLogSavePath;
  static bool isInit;
  spdlog::logger *mLogger;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QLogger::LogModes)
#endif // QLOGGER_H
